$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + '/menu/getMenuList',
        datatype: "json",
        colModel: [         
        { label: 'ID', name: 'id', width: 30, key: true },
        { label: '菜单名称', name: 'name', width: 60 },
        { label: '层级', name: 'level', width: 20 },
        { label: 'url', name: 'url', width: 100 },
        {
          label:"权限",name:"roleCode",width: 120,formatter: function(value, options, row){
            return value.replace(/[0][0][0][0]/g, '超级管理员').replace(/[0][0][0][1]/g, '系统管理员').replace(/[0][0][0][2]/g, '商家').replace(/[0][0][0][3]/g, '财务').replace(/[0][0][0][4]/g, '试客');
        }
    },
    {label:"icon",name:"icon",width: 30},
    { label: '操作', name: 'status', width: 80, formatter: function(value, options, row){
        return '<span class="btn btn-info" onclick="update('+row.id+')">修改</span><span class="btn btn-danger" onclick="del('+row.id+')">删除</span>';
    }},
    ],
    viewrecords: true,
    height: 555,
    rowNum: 10,
    rowList : [10,30,50],
    rownumbers: true, 
    rownumWidth: 25, 
    autowidth:true,
    multiselect: true,
    pager: "#jqGridPager",
    jsonReader : {
        root: "data.dataList",
        page: "data.currentPage",
        records: "data.totalNum",
        total: "data.total"
    },
    prmNames : {
        page:"pageNo", 
        rows:"pageSize", 
        order: "order"
    },
    gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});
function del(id){
      console.log("id "+id);
};
function update(id){
    
    console.log("id "+id);
};
var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            key: null
        },
        showList: true,
        title: null
    },
    methods: {
        query: function () {
            vm.reload();
        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{ 
                postData:{'key': vm.q.key},
                page:page
            }).trigger("reloadGrid");
        },
         
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.menu = {parentName:null,parentId:0,type:1,orderNum:0};
            vm.getMenu();
        },
        update: function () {
            var menuId = getMenuId();
            if(menuId == null){
                return ;
            }

            $.get(baseURL + "sys/menu/info/"+menuId, function(r){
                vm.showList = false;
                vm.title = "修改";
                vm.menu = r.menu;

                vm.getMenu();
            });
        },
        del: function () {
            var menuId = getMenuId();
            if(menuId == null){
                return ;
            }

            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/menu/delete/" + menuId,
                    success: function(r){
                        if(r.code === 0){
                            alert('操作成功', function(){
                                location.reload();
                                // vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        saveOrUpdate: function () {
            if(vm.validator()){
                return ;
            }

            var url = vm.menu.menuId == null ? "sys/menu/save" : "sys/menu/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.menu),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        }

        
    }
});